;
(function ($) {
	$.fn.slidebanner = function (settings) {
		var _defaultSettings = {
			slideTime: 500,
			waitTime:5000
		};
		var _settings = $.extend(true, _defaultSettings, settings);
		
		var $container;
		var $pager;
		var timer;
		
		if($(this).length > 0 && $(this).first().find("img").length > 0){
			$container = $(this).first();
			$pager = $("<ul></ul>");
			
			$container.css("position","relative");
			$pager.addClass("slidepager").insertAfter($container);
			
			$container.find("img").each(function(k,v){
				var $wrap = $("<div></div>").css({
					position : "absolute",
					top : "0px",
					left : "0px",
					zIndex : "0",
					overflow : "hidden"
				}).addClass("slideItem").attr("data-no",k).hide();
				$(this).css({
					position : "absolute",
					top : "0px",
					left : "0px"
				}).wrap($wrap);
				
				
				var $li = $("<li></li>").attr("data-no",k);
				var $a = $("<a></a>").click(function(){
					if($(this).hasClass("disabled") || $(this).hasClass("active")) return false;
					
					clearInterval(timer);
					var $curr = $(".slideItem.curr");
					var $next = $(".slideItem:eq(" + k + ")");
					if($next.length <= 0)
						$next = $(".slideItem").first();
					doSlide(_settings.slideTime, $curr, $next);
					
					runSlide();
				});
				$li.append($a);
				$pager.append($li);
				
			});
			$(window).resize(function(){
				slideResize();
			});
			
			
			$(".slideItem").first().css("z-index", "10").addClass("curr").show();
			$(".slideItem:first img").one("load",function(){
				slideResize();
			});
			if($(".slideItem:first img")[0].complete) $(".slideItem:first img").trigger("load");
			$(".slidepager li:first a").addClass("active");
			
			runSlide();
		}
		
		function slideResize(){
			var wW = $(window).width();
			var wH = $(window).height() - $("#header").height() - $("#header_menu").height();
			
			$container.width(wW).height(wH);
			$(".slideItem").each(function(){
				$(this).width(wW).height(wH);
				imgResize($(this).find("img").first());
			});
		}
		function imgResize($img){
			var wW = $container.width();
			var wH = $container.height();
			
			$img.removeAttr("width");
			$img.removeAttr("height");
			$img.css("width","auto");
			$img.css("height","auto");
			var imgW = $img.width();
			var imgH = $img.height();
			
			
			if(imgW > 0 && imgH > 0){
				//計算縮放比例
				var w = wW / imgW;
				var h = wH / imgH;
				var pre = 1;
				pre = (w > h)? w : h;
				
				var nw = Math.floor(imgW * pre);
				var nh = Math.floor(imgH * pre);
				
				var diffw = (nw - wW) / 2;
				var diffh = (nh - wH) / 2;
				
				$img.width(nw).height(nh).css({
					"top" : (0-diffh) + "px",
					"left" : (0-diffw) + "px"
				});

			}
		}
		
		function runSlide(){
			timer = setInterval(function(){
				var $curr = $(".slideItem.curr");
				var $next = $(".slideItem.curr").next(".slideItem");
				if($next.length <= 0)
					$next = $(".slideItem").first();
				doSlide(_settings.slideTime, $curr, $next);
			},_settings.slideTime + _settings.waitTime);
		}
		function doSlide(slideTime, $curr, $next){
			$(".slidepager li a").addClass("disabled");
			$(".slidepager li a").removeClass("active");
			$(".slidepager li:eq(" + $next.attr("data-no") + ") a").addClass("active");
			
			if(parseInt($next.attr("data-no"),10) > parseInt($curr.attr("data-no"),10)){
				$curr.css("z-index", "0").animate({
					marginLeft : "-" + $container.width() + "px"
				},slideTime,function(){
					});	
				$next.css("z-index", "10").show(0,function(){
					slideResize();
					$(this).css("margin-left",$container.width() + "px");
				}).animate({
					marginLeft : "0px"
				},slideTime,function(){
					$curr.hide().removeClass("curr").css("margin-left","0px");
					$next.addClass("curr");
					$(".slidepager li a").removeClass("disabled");
				});	
			} else{
				$curr.css("z-index", "0").animate({
					marginLeft : $container.width() + "px"
				},slideTime,function(){
				});	
				
				$next.css("z-index", "10").show(0,function(){
					slideResize();
					$(this).css("margin-left", "-" + $container.width() + "px");
				}).animate({
					marginLeft : "0px"
				},slideTime,function(){
					$curr.hide().removeClass("curr").css("margin-left","0px");
					$next.addClass("curr");
					$(".slidepager li a").removeClass("disabled");
				});	
			}
		}
	};
})(jQuery);